package com.example.controller;

import com.example.model.*;
import com.example.service.IOrderDishService;
import com.example.service.IUserOwnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Hello
 * @since 2024-07-03
 */
@RestController
@RequestMapping("/orderDish")
@Api(tags = "Order Dish Management")
public class OrderDishController {

    @Autowired
    private IOrderDishService orderDishService;
    @Autowired
    private IUserOwnService userOwnService;
    @PostMapping("/createOrder")
    @ApiOperation(value = "Create a new order")
    public Result<String> createOrder(@RequestBody OrderRequest orderRequest) {
        OrderUser orderUser = orderRequest.getOrderUser();
        Integer userOwnId = orderUser.getUserOwnId();
        orderUser.setOrderTime(LocalDateTime.now());
        if(!userOwnService.updateByUserOwnId(userOwnId)) {
            return Result.fail("优惠券已使用");
        }

        List<OrderDishHelper> orderDishHelpers = orderRequest.getOrderDishes();

        orderDishService.createOrder(orderUser, orderDishHelpers);
        return Result.success("Order created successfully");
    }
}